require.config({baseUrl: '/js/lib',urlArgs: "v0.0.3"});
require(['zepto', 'lib'], function($, lib) {
  var fn = {}, lib = new lib();
  $.extend(fn, {
    goodsTotalNum: 0,
    orderMoney: 0,
    //payTypeId: sessionStorage.payTypeId || '18',
    payTypeId: '17',
    invoiceInfo: JSON.parse(sessionStorage.invoiceInfo || '{"invoiceType": 0}'),
    orderConfirmInfo: JSON.parse(sessionStorage.orderConfirmInfo || '{}'),
    gNum: lib.getUrlParam('gNum'), /*商品数量*/
    gId: lib.getUrlParam('gId'), /*商品id*/
    sId: lib.getUrlParam('sId'), /*商铺id*/
    oId: lib.getUrlParam('oId'), /*orderId*/
    ot: lib.getUrlParam('ot'), /*订单类型*/
    mt: lib.getUrlParam('mt') ||　1, /*下单方式*/
    init: function(){
      var _t = this;
      _t.initPage();
    },
    initPage: function(){
      var _t = this;
      var h = '';
      lib.ajx(lib.getReq().ser + '/order/orderConfirmInfo.action', {makeType: _t.mt, goodsId: _t.gId, storageId: _t.sId, goodsNum: _t.gNum, oId: _t.oId, orderType: _t.ot}, function(data){
        if(data.infocode === "0"){
          data = data.info;
          $.each(data, function(K, V) {
            h +=`<ul class="ui-order-info ui-mar-b-20">
                  <li>
                    <i>取货地址：</i>
                    <i>${V.storageAddress}</i>
                  </li>
                </ul>
                <div class="ui-goods-info ui-mar-b-20">
                  <div class="ui-shop-name"><i>${V.storageName}</i><i class="ui-r-b" style="margin-top: 0.62rem;margin-left: 5px"></i></div>
                  <div class="ui-g-detail">
                    <ul>`
            $.each(V.goodsList, function(k, v){
              _t.goodsTotalNum += v.goodsNum;
              _t.orderMoney += v.goodsNum * Number(v.sellPrice);
              h += `<li>
                      <div class="ui-g-img"><img src="${lib.getReq().imgPath + v.mainPictureJpg}" /></div>
                      <ul class="ui-g-desc">
                        <li>${v.chName}</li>
                        <li>
                          <i class="ui-g-price">￥${v.sellPrice}</i>
                          <i class="ui-g-num">x${v.goodsNum}</i>
                        </li>
                      </ul>
                    </li>`;
            
            })
            
            h += "</ul></div></div>";
          	
          });
          h +=`<ul class="ui-order-other ui-mar-b-20">
                <li>
                  <a href="#">
                    <i>支付方式：</i>`;
                    
          if(lib.checkWeiXin()){
            _t.payTypeId = 18
            h += `<i class="ui-weixin-icon" style="margin-top: 0.52rem;float: right;"></i>
                    <i style="float:right;margin-right: 0.1rem">微信支付</i>`
          } else {
            _t.payTypeId = 17
            h += `<i class="ui-zhifubao-icon" style="margin-top: 0.58rem;float: right;"></i>
                    <i style="float:right;margin-right: 0.1rem">支付宝</i>`
          }
          h +=          `
                  </a>
                </li>
                <li>
                  <a href="/invoice.html?backUrl=${lib.getBackUrl()}">
                    <i>发票信息：</i>
                    <i class="ui-r-g" style="margin-top: 0.62rem;float: right;"></i>
                    <i style="float:right;margin-right: 0.6rem">${_t.invoiceInfo.invoiceType === '2' ? '个人发票' : (_t.invoiceInfo.invoiceType === '1' ? '单位发票' : '不开发票')}</i>
                  </a>
                </li>
                <li style="display:none">
                  <a href="/home/coupon.html?backUrl=${lib.getBackUrl()}">
                    <i>优惠券：</i>
                    <i class="ui-r-g" style="margin-top: 0.62rem;float: right;"></i>
                    <i style="float:right;margin-right: 0.6rem"></i>
                  </a>
                </li>
              </ul>
              
              <ul class="ui-order-i ui-mar-b-20">
                <li>
                    <i>商品金额：</i>
                    <i>共${_t.goodsTotalNum}件商品   共计￥${_t.orderMoney.toFixed(2)}</i>
                  </a>
                </li>
                <li class="ui-border-bottom">
                    <i>运费金额：</i>
                    <i>￥0</i>
                  </a>
                </li>
                <li>
                    <i>总计金额：</i>
                    <i>￥${_t.orderMoney.toFixed(2)}</i>
                  </a>
                </li>
              </ul>
              <div style="clear: both;"></div>
              <!--订单金额end-->
              <botton class="ui-red-botton" style="position: fixed;bottom: 0;">去支付</botton>`
          $('body').append(h);
          $(".ui-red-botton").tap(function(){
            lib.onLoading();
            lib.ajx(lib.getReq().ser + '/order/makeOrder.action', Object.assign({makeType: _t.mt, goodsId: _t.gId, storageId: _t.sId, goodsNum: _t.gNum, oId: _t.oId, orderType: _t.ot, payTypeId: _t.payTypeId}, _t.invoiceInfo), function(data){
              if(data.infocode === "0"){
                if(_t.payTypeId == 18){
                  if(lib.getIsOnline()){
                      location.href = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' + lib.getReq().appid + 
                      '&redirect_uri=http://api.chengshijiugui.com/csjgmanager/pay/wxPay.action&response_type=code&scope=snsapi_base&state='+ data.info + '#wechat_redirect';
                      return;
                  }else{
                    location.href = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' + lib.getReq().appid + 
                    '&redirect_uri=http://api.chengshijiugui.com/csjgmanager/pay/wxPay.action&response_type=code&scope=snsapi_base&state='+ data.info  + '#wechat_redirect';
                  }
                }else if(_t.payTypeId == 17){
                  // alert('目前只支持微信支付，请在微信中打开');
                  // return;
                  lib.ajx(lib.getReq().ser + '/pay/alipay.action', {orderId: data.info, originType: 1}, function(data){
                    if(data.infocode === "0"){
                      location.href = data.info;
                    }else{
                      lib.showMessage(data.info);
                    }
                  }, function(){
                    lib.showMessage('网络错误,请稍后再试');
                  });
                }
                return;
              }else if(data.infocode === "2"){
                location.href = "/login.html?backUrl=" + lib.getBackUrl();
                return;
              }
              lib.showMessage(data.info);
              lib.offLoading();
            }, function(){
              lib.showMessage('网络错误,请稍后再试');
              lib.offLoading();
            })
          });
          return;
        }else if(data.infocode === "2"){
          location.href = "/login.html?backUrl=" + lib.getBackUrl();
          return;
        }
        lib.showMessage(data.info);
      }, function(){
        lib.showMessage('网络错误,请稍后再试');
      })
    }
  });
  
  fn.init();
});